package com.delu.mq;

import com.alibaba.fastjson.JSON;
import com.delu.callback.AlipayCallback;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Component
@Slf4j
public class RocketMQSendUtils {
//    @Autowired
//    private RocketMQTemplate rocketMQTemplate;
//    private static Logger log = LoggerFactory.getLogger(RocketMQSendUtils.class);
//    /**
//     * @param topicAndTags     主题
//     * @param rocketMQSendBean 数据
//     * @param delayLevel       延迟等级 RocketMQ不支持任意时间的延时，只支持以下几个固定的延时等级 private String messageDelayLevel = "1s 5s 10s 30s 1m 2m 3m 4m 5m 6m 7m 8m 9m 10m 20m 30m 1h 2h";
//     * @return void+
//     * @description
//     * @author middo
//     * @date 2021/7/12 11:41
//     */
//    public void sendDelay(String topicAndTags, RocketMQSendBean<?> rocketMQSendBean, int delayLevel) {
//        SendResult result = null;
//        try {
//            result = rocketMQTemplate.syncSend(topicAndTags, MessageBuilder.withPayload(rocketMQSendBean).build(), RocketMQConstant.TIME_OUT_COUNT, delayLevel);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        log.info("发送时间：", LocalDateTime.now());
//        log.info("发送时间内容: ", JSON.toJSONString(result));
//    }
//
//    /**
//     * @param topicAndTags
//     * @param rocketMQSendBean
//     * @param hashKey
//     * @return void
//     * @description 异步发送消息
//     * @author Ben
//     * @date 2021/7/6/0006 9:58:03
//     */
//    public void asyncSendOrderly(String topicAndTags, RocketMQSendBean<?> rocketMQSendBean, String hashKey) {
//        rocketMQTemplate.asyncSendOrderly(topicAndTags, MessageBuilder.withPayload(rocketMQSendBean).build(), hashKey, new SendCallback() {
//            @Override
//            public void onSuccess(SendResult sendResult) {
//                log.info("send successful");
//            }
//
//            @Override
//            public void onException(Throwable throwable) {
//                log.info("send fail; {}", throwable.getMessage());
//            }
//        });
//    }
}

